library(texreg)



# extension for feglm objects (alpaca package)
extract.feglm <- function(
  model
  , include.deviance = TRUE
  , include.nobs = TRUE
  , include.groups = TRUE, ...) {
  s <- summary(model, ...)
  names <- rownames(s$cm)
  co <- s$cm[, 1]
  se <- s$cm[, 2]
  pval <- s$cm[, 4]
  
  gof <- numeric()
  gof.names <- character()
  gof.decimal <- logical()
  
  if (include.deviance == TRUE) {
    dev <- s$deviance
    gof <- c(gof, dev)
    gof.names <- c(gof.names, "Deviance")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  
  if (include.nobs == TRUE) {
    n <- s$nobs['nobs']
    gof <- c(gof, n)
    gof.names <- c(gof.names, "Num.\\ obs.")
    gof.decimal <- c(gof.decimal, FALSE)
  }
  
  if (include.groups == TRUE) {
    grp <- s$lvls.k
    grp.names <- paste0("Num groups:", names(grp))
    gof <- c(gof, grp)
    gof.names <- c(gof.names, grp.names)
    gof.decimal <- c(gof.decimal, rep(FALSE, length(grp)))
  }
  
  tr <- createTexreg(
    coef.names = names,
    coef = co,
    se=se,
    pvalues = pval,
    gof.names = gof.names,
    gof = gof,
    gof.decimal = gof.decimal
  )
  return(tr)
}

setMethod("extract", signature = className("feglm", "alpaca"), definition = extract.feglm)
